<?php 
//DepartmentSearch.php
//@author William Harris

require_once("overhead.php");
require_once("Search.php");

class DepartmentSearch implements Search{
	var $name;
	var $code;
	var $departments;


// Setters required based on input
	public function setName($inName){
		$this->name = $inName;
	}	
	public function setCode($inCode){
		$this->code = $inCode;
	}	
	public function clear(){//Sets all objects to nulls
		$this->code = null;
		$this->name = null;
		$this->departments = null;
	}
	public function query(){ //Sets the Department array of this instance to the results of this query 
		$queryString = "select Department.* from Department"; 
		
		$depSearchResults = array();
		
		$queryArray = array(); 
		
		if (isset($this->name)){
			$queryArray[] =	"Name LIKE '%". $this->name ."%'";	
		}
		if (isset($this->code)){
			$queryArray[] =	"Code LIKE '%". $this->code ."%'";				
		}
		
		if($queryArray){
		$queryString = "select Department.* from Department where "; 
		$queryString = $queryString.implode(" and ", $queryArray); //Join all of the search terms if they appear in the if statement 
		}
		
		//var_dump($queryString);
		$result = mysql_query($queryString);
		
		if($result){
			
			$i = 0;
			while($row = mysql_fetch_assoc($result)){
				
				$dep = new Department();
				$dep->retrieveFromID($row['ID']); //Department Id
				$dep->setName($row['Name']);
				$dep->setCode($row['Code']); //coresponds to the Department code (for example, "CS" for the Computer Science Department)
				
				$depSearchResults[$i] = $dep;
				
				$i++;
			}
			
			$this->departments = $depSearchResults;
			
			return true;
		} else {

			return false;
		}
	}
		
	public function getAllResults(){
		return $this->departments; //Return the array of Departments from query
	}
	public function getResult($index){
		return $this->departments[$index]; //Return the Department object at the position index
	}
	public function sort($fieldName){
	/* not implemented yet. Will sort deps*/ 
	}
		
}
